/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     SummerGift   first version
 */

#include "board.h"
#ifdef BSP_USING_SPI
#include "drv_spi.h"
#endif

#define VDD_3V3_SENSORS_EN_Pin GPIO_PIN_3
#define VDD_3V3_SENSORS_EN_GPIO_Port GPIOE
#define UART8_TX_Pin GPIO_PIN_1
#define UART8_TX_GPIO_Port GPIOE
#define UART8_RX_Pin GPIO_PIN_0
#define UART8_RX_GPIO_Port GPIOE
#define I2C1_SCL_Pin GPIO_PIN_8
#define I2C1_SCL_GPIO_Port GPIOB
#define DRDY1_ICM20689_Pin GPIO_PIN_4
#define DRDY1_ICM20689_GPIO_Port GPIOB
#define FMU_CAP2_Pin GPIO_PIN_3
#define FMU_CAP2_GPIO_Port GPIOB
#define SPEKTRUM_PWR_Pin GPIO_PIN_4
#define SPEKTRUM_PWR_GPIO_Port GPIOE
#define I2C1_SDA_Pin GPIO_PIN_9
#define I2C1_SDA_GPIO_Port GPIOB
#define USART1_RX_Pin GPIO_PIN_7
#define USART1_RX_GPIO_Port GPIOB
#define USART1_TX_Pin GPIO_PIN_6
#define USART1_TX_GPIO_Port GPIOB
#define USART6_CTS_Pin GPIO_PIN_15
#define USART6_CTS_GPIO_Port GPIOG
#define BMI055_A_CS_Pin GPIO_PIN_10
#define BMI055_A_CS_GPIO_Port GPIOG
#define USART2_RX_Pin GPIO_PIN_6
#define USART2_RX_GPIO_Port GPIOD
#define UART4_RX_Pin GPIO_PIN_0
#define UART4_RX_GPIO_Port GPIOD
#define USART6_RX_Pin GPIO_PIN_9
#define USART6_RX_GPIO_Port GPIOG
#define USART2_TX_Pin GPIO_PIN_5
#define USART2_TX_GPIO_Port GPIOD
#define UART4_TX_Pin GPIO_PIN_1
#define UART4_TX_GPIO_Port GPIOD
#define DRDY5_BMI055_GYRO_Pin GPIO_PIN_13
#define DRDY5_BMI055_GYRO_GPIO_Port GPIOC
#define USART2_RTS_Pin GPIO_PIN_4
#define USART2_RTS_GPIO_Port GPIOD
#define USART2_CTS_Pin GPIO_PIN_3
#define USART2_CTS_GPIO_Port GPIOD
#define FMU_SPARE_4_Pin GPIO_PIN_0
#define FMU_SPARE_4_GPIO_Port GPIOI
#define LED_BLUE_Pin GPIO_PIN_7
#define LED_BLUE_GPIO_Port GPIOC
#define ICM20689_CS_Pin GPIO_PIN_2
#define ICM20689_CS_GPIO_Port GPIOF
#define USART6_RTS_Pin GPIO_PIN_8
#define USART6_RTS_GPIO_Port GPIOG
#define LED_GREEN_Pin GPIO_PIN_6
#define LED_GREEN_GPIO_Port GPIOC
#define ICM20602_CS_Pin GPIO_PIN_3
#define ICM20602_CS_GPIO_Port GPIOF
#define BMI055_G_CS_Pin GPIO_PIN_4
#define BMI055_G_CS_GPIO_Port GPIOF
#define VDD_3V3_SD_CARD_EN_Pin GPIO_PIN_7
#define VDD_3V3_SD_CARD_EN_GPIO_Port GPIOG
#define VDD_5V_WIFI_EN_Pin GPIO_PIN_6
#define VDD_5V_WIFI_EN_GPIO_Port GPIOG
#define UART7_RX_Pin GPIO_PIN_6
#define UART7_RX_GPIO_Port GPIOF
#define FRAM_CS_Pin GPIO_PIN_5
#define FRAM_CS_GPIO_Port GPIOF
#define LED_B1_Pin GPIO_PIN_12
#define LED_B1_GPIO_Port GPIOH
#define VDD_5V_RC_EN_Pin GPIO_PIN_5
#define VDD_5V_RC_EN_GPIO_Port GPIOG
#define nVDD_5V_PERIPH_EN_Pin GPIO_PIN_4
#define nVDD_5V_PERIPH_EN_GPIO_Port GPIOG
#define VBUS_nVALID_Pin GPIO_PIN_3
#define VBUS_nVALID_GPIO_Port GPIOG
#define MS5611_CS_Pin GPIO_PIN_10
#define MS5611_CS_GPIO_Port GPIOF
#define LED_G1_Pin GPIO_PIN_11
#define LED_G1_GPIO_Port GPIOH
#define LED_R1_Pin GPIO_PIN_10
#define LED_R1_GPIO_Port GPIOH
#define DRDY7_EXTERNAL1_Pin GPIO_PIN_15
#define DRDY7_EXTERNAL1_GPIO_Port GPIOD
#define VDD_BRICK2_nVALID_Pin GPIO_PIN_2
#define VDD_BRICK2_nVALID_GPIO_Port GPIOG
#define VDD_5V_SENS_Pin GPIO_PIN_0
#define VDD_5V_SENS_GPIO_Port GPIOC
#define SCALED_V3V3_Pin GPIO_PIN_1
#define SCALED_V3V3_GPIO_Port GPIOC
#define VDD_BRICK_nVALID_Pin GPIO_PIN_1
#define VDD_BRICK_nVALID_GPIO_Port GPIOG
#define BATT_CURRENT_SENS_Pin GPIO_PIN_1
#define BATT_CURRENT_SENS_GPIO_Port GPIOA
#define BATT_VOLTAGE_SENS_Pin GPIO_PIN_0
#define BATT_VOLTAGE_SENS_GPIO_Port GPIOA
#define SPARE2_ADC1_Pin GPIO_PIN_4
#define SPARE2_ADC1_GPIO_Port GPIOA
#define SPARE1_ADC1_Pin GPIO_PIN_4
#define SPARE1_ADC1_GPIO_Port GPIOC
#define VDD_5V_HIPOWER_nOC_Pin GPIO_PIN_13
#define VDD_5V_HIPOWER_nOC_GPIO_Port GPIOF
#define USART3_RTS_Pin GPIO_PIN_12
#define USART3_RTS_GPIO_Port GPIOD
#define USART3_CTS_Pin GPIO_PIN_11
#define USART3_CTS_GPIO_Port GPIOD
#define DRDY6_BMI055_ACC_Pin GPIO_PIN_10
#define DRDY6_BMI055_ACC_GPIO_Port GPIOD
#define BATT2_VOLTAGE_SENS_Pin GPIO_PIN_2
#define BATT2_VOLTAGE_SENS_GPIO_Port GPIOA
#define FMU_CAP1_Pin GPIO_PIN_5
#define FMU_CAP1_GPIO_Port GPIOA
#define DRDY4_ICM20602_Pin GPIO_PIN_5
#define DRDY4_ICM20602_GPIO_Port GPIOC
#define nVDD_5V_HIPOWER_EN_Pin GPIO_PIN_12
#define nVDD_5V_HIPOWER_EN_GPIO_Port GPIOF
#define UART7_TX_Pin GPIO_PIN_8
#define UART7_TX_GPIO_Port GPIOE
#define USART3_RX_Pin GPIO_PIN_9
#define USART3_RX_GPIO_Port GPIOD
#define PD8_USART3_TX_USART3_Pin GPIO_PIN_8
#define PD8_USART3_TX_USART3_GPIO_Port GPIOD
#define BATT2_CURRENT_SENS_Pin GPIO_PIN_3
#define BATT2_CURRENT_SENS_GPIO_Port GPIOA
#define HEATER_EN_Pin GPIO_PIN_7
#define HEATER_EN_GPIO_Port GPIOA
#define LED_RED_Pin GPIO_PIN_1
#define LED_RED_GPIO_Port GPIOB
#define RSSI_IN_Pin GPIO_PIN_0
#define RSSI_IN_GPIO_Port GPIOB
#define DRDY8_NC_Pin GPIO_PIN_7
#define DRDY8_NC_GPIO_Port GPIOE
#define VDD_5V_PERIPH_nOC_Pin GPIO_PIN_15
#define VDD_5V_PERIPH_nOC_GPIO_Port GPIOE
#define nSPI5_RESET_EXTERNAL1_Pin GPIO_PIN_10
#define nSPI5_RESET_EXTERNAL1_GPIO_Port GPIOB
#define FMU_CAP3_Pin GPIO_PIN_11
#define FMU_CAP3_GPIO_Port GPIOB
#define DRDY2_BMI055_GYRO_Pin GPIO_PIN_14
#define DRDY2_BMI055_GYRO_GPIO_Port GPIOB
#define DRDY3_BMI055_ACC_Pin GPIO_PIN_15
#define DRDY3_BMI055_ACC_GPIO_Port GPIOB

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 216;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Activate the Over-Drive mode
  */
  if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
                              |RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_USART6
                              |RCC_PERIPHCLK_UART7|RCC_PERIPHCLK_UART8
                              |RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_I2C2
                              |RCC_PERIPHCLK_I2C3|RCC_PERIPHCLK_I2C4
                              |RCC_PERIPHCLK_SDMMC1|RCC_PERIPHCLK_CLK48;
  PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  PeriphClkInitStruct.Usart6ClockSelection = RCC_USART6CLKSOURCE_PCLK2;
  PeriphClkInitStruct.Uart7ClockSelection = RCC_UART7CLKSOURCE_PCLK1;
  PeriphClkInitStruct.Uart8ClockSelection = RCC_UART8CLKSOURCE_PCLK1;
  PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  PeriphClkInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_PCLK1;
  PeriphClkInitStruct.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  PeriphClkInitStruct.I2c4ClockSelection = RCC_I2C4CLKSOURCE_PCLK1;
  PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL;
  PeriphClkInitStruct.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_CLK48;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

#define MS5611_CS_PORT GPIOF
#define MS5611_CS_PIN GPIO_PIN_10

#define ICM20689_CS_PORT GPIOF
#define ICM20689_CS_PIN GPIO_PIN_2

#define ICM20602_CS_PORT GPIOF
#define ICM20602_CS_PIN GPIO_PIN_3

#define BMI055_G_CS_PORT GPIOF
#define BMI055_G_CS_PIN GPIO_PIN_4

#define BMI055_A_CS_PORT GPIOG
#define BMI055_A_CS_PIN GPIO_PIN_10

#define FRAM_CS_PORT GPIOF
#define FRAM_CS_PIN GPIO_PIN_5

#ifdef BSP_USING_SPI
/*** SPI1 BUS and device
SPI1_MOSI: PA7
SPI1_MISO: PA6
SPI1_SCK : PA5
*/
int stm32_hw_spi_init(void)
{
    /* rt name max is 8 (RT_NAME_MAX   8) */
    rt_hw_spi_device_attach("spi1", "spi1_d1", ICM20689_CS_PORT, ICM20689_CS_PIN);

    /* rt name max is 8 (RT_NAME_MAX   8) */
    rt_hw_spi_device_attach("spi1", "spi1_d2", ICM20602_CS_PORT, ICM20602_CS_PIN);

    /* rt name max is 8 (RT_NAME_MAX   8) */
    rt_hw_spi_device_attach("spi1", "spi1_d3", BMI055_G_CS_PORT, BMI055_G_CS_PIN);

    /* rt name max is 8 (RT_NAME_MAX   8) */
    rt_hw_spi_device_attach("spi1", "spi1_d4", BMI055_A_CS_PORT, BMI055_A_CS_PIN);

    rt_hw_spi_device_attach("spi2", "spi2_d1", FRAM_CS_PORT, FRAM_CS_PIN);

    rt_hw_spi_device_attach("spi4", "spi4_d1", MS5611_CS_PORT, MS5611_CS_PIN);

    return 1;
}
INIT_DEVICE_EXPORT(stm32_hw_spi_init);
#endif

#define RT_APP_PART_ADDR 0x08008000
/**
Function ota_app_vtor_reconfig
Description Set Vector Table base location to the start addr of app(RT_APP_PART_ADDR).
*/
static int ota_app_vtor_reconfig(void)
{
    #define NVIC_VTOR_MASK 0x3FFFFF80
    /* Set the Vector Table base location by user application firmware definition */
    SCB->VTOR = RT_APP_PART_ADDR & NVIC_VTOR_MASK;

    return 0;
}
INIT_BOARD_EXPORT(ota_app_vtor_reconfig);

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static int MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOG_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOI_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOF_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(VDD_3V3_SENSORS_EN_GPIO_Port, VDD_3V3_SENSORS_EN_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, I2C1_SCL_Pin|LED_RED_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(SPEKTRUM_PWR_GPIO_Port, SPEKTRUM_PWR_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(BMI055_A_CS_GPIO_Port, BMI055_A_CS_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOF, ICM20689_CS_Pin|ICM20602_CS_Pin|BMI055_G_CS_Pin|FRAM_CS_Pin
                          |MS5611_CS_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOG, VDD_3V3_SD_CARD_EN_Pin|VDD_5V_WIFI_EN_Pin|VDD_5V_RC_EN_Pin|nVDD_5V_PERIPH_EN_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOH, LED_B1_Pin|LED_G1_Pin|LED_R1_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(nVDD_5V_HIPOWER_EN_GPIO_Port, nVDD_5V_HIPOWER_EN_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(HEATER_EN_GPIO_Port, HEATER_EN_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(nSPI5_RESET_EXTERNAL1_GPIO_Port, nSPI5_RESET_EXTERNAL1_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin : VDD_3V3_SENSORS_EN_Pin */
  GPIO_InitStruct.Pin = VDD_3V3_SENSORS_EN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(VDD_3V3_SENSORS_EN_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : I2C1_SCL_Pin nSPI5_RESET_EXTERNAL1_Pin */
  GPIO_InitStruct.Pin = I2C1_SCL_Pin|nSPI5_RESET_EXTERNAL1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : DRDY1_ICM20689_Pin FMU_CAP2_Pin I2C1_SDA_Pin FMU_CAP3_Pin
                           DRDY2_BMI055_GYRO_Pin DRDY3_BMI055_ACC_Pin */
  GPIO_InitStruct.Pin = DRDY1_ICM20689_Pin|FMU_CAP2_Pin|I2C1_SDA_Pin|FMU_CAP3_Pin
                          |DRDY2_BMI055_GYRO_Pin|DRDY3_BMI055_ACC_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pin : SPEKTRUM_PWR_Pin */
  GPIO_InitStruct.Pin = SPEKTRUM_PWR_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(SPEKTRUM_PWR_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : BMI055_A_CS_Pin */
  GPIO_InitStruct.Pin = BMI055_A_CS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(BMI055_A_CS_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : DRDY5_BMI055_GYRO_Pin DRDY4_ICM20602_Pin */
  GPIO_InitStruct.Pin = DRDY5_BMI055_GYRO_Pin|DRDY4_ICM20602_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pin : FMU_SPARE_4_Pin */
  GPIO_InitStruct.Pin = FMU_SPARE_4_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(FMU_SPARE_4_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : LED_BLUE_Pin LED_GREEN_Pin */
  GPIO_InitStruct.Pin = LED_BLUE_Pin|LED_GREEN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pins : ICM20689_CS_Pin ICM20602_CS_Pin BMI055_G_CS_Pin FRAM_CS_Pin
                           MS5611_CS_Pin */
  GPIO_InitStruct.Pin = ICM20689_CS_Pin|ICM20602_CS_Pin|BMI055_G_CS_Pin|FRAM_CS_Pin
                          |MS5611_CS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

  /*Configure GPIO pins : VDD_3V3_SD_CARD_EN_Pin VDD_5V_WIFI_EN_Pin VDD_5V_RC_EN_Pin nVDD_5V_PERIPH_EN_Pin */
  GPIO_InitStruct.Pin = VDD_3V3_SD_CARD_EN_Pin|VDD_5V_WIFI_EN_Pin|VDD_5V_RC_EN_Pin|nVDD_5V_PERIPH_EN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  /*Configure GPIO pins : LED_B1_Pin LED_G1_Pin LED_R1_Pin */
  GPIO_InitStruct.Pin = LED_B1_Pin|LED_G1_Pin|LED_R1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);

  /*Configure GPIO pins : VBUS_nVALID_Pin VDD_BRICK2_nVALID_Pin VDD_BRICK_nVALID_Pin */
  GPIO_InitStruct.Pin = VBUS_nVALID_Pin|VDD_BRICK2_nVALID_Pin|VDD_BRICK_nVALID_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  /*Configure GPIO pins : DRDY7_EXTERNAL1_Pin DRDY6_BMI055_ACC_Pin */
  GPIO_InitStruct.Pin = DRDY7_EXTERNAL1_Pin|DRDY6_BMI055_ACC_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /*Configure GPIO pin : VDD_5V_HIPOWER_nOC_Pin */
  GPIO_InitStruct.Pin = VDD_5V_HIPOWER_nOC_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(VDD_5V_HIPOWER_nOC_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : FMU_CAP1_Pin */
  GPIO_InitStruct.Pin = FMU_CAP1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(FMU_CAP1_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : nVDD_5V_HIPOWER_EN_Pin */
  GPIO_InitStruct.Pin = nVDD_5V_HIPOWER_EN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(nVDD_5V_HIPOWER_EN_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : HEATER_EN_Pin */
  GPIO_InitStruct.Pin = HEATER_EN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(HEATER_EN_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : LED_RED_Pin */
  GPIO_InitStruct.Pin = LED_RED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LED_RED_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : DRDY8_NC_Pin */
  GPIO_InitStruct.Pin = DRDY8_NC_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(DRDY8_NC_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : VDD_5V_PERIPH_nOC_Pin */
  GPIO_InitStruct.Pin = VDD_5V_PERIPH_nOC_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(VDD_5V_PERIPH_nOC_GPIO_Port, &GPIO_InitStruct);

  return 0;
}
INIT_BOARD_EXPORT(MX_GPIO_Init);